<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>动画脚本演示 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,bootstrap,bootstrap-checkbox,font-awesome,web-icons,jstree,layer,haoutil,nprogress,toastr,admui,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <link href="css/divGraphic.css" rel="stylesheet" />
    <style>
      .viewResult {
        top: 10px;
        right: 10px;
        bottom: auto;
        left: auto;
        max-width: 420px;
        overflow: auto;
        font-size: 28px;
      }

      .contentUl {
        padding: 0;
        text-align: left;
        font-size: 20px;
        margin-left: 10px;
      }

      .btn i {
        margin-right: 5px;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <div id="viewReset" class="infoview" style="overflow: auto; left: 10px; top: 10px">
      <div class="btn-group">
        <button id="btn_start" type="button" class="btn btn-primary"><i class="fa fa-play-circle-o"></i>开始</button>
        <button id="btn_pause" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-pause-circle-o"></i>暂停</button>
        <button id="btn_proceed" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-play-circle-o"></i>继续</button>
        <button id="btn_stop" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-stop-circle-o"></i>停止</button>
      </div>

      <ul id="treeOverlays" style="padding: 0"></ul>
      <br />
      总时长: <span id="alltimes">0s</span> &nbsp;&nbsp;
      <br />
      当前:&nbsp;&nbsp;<span id="thisStep">无</span> &nbsp;&nbsp;
      <span id="thistimes"></span>
    </div>

    <script type="text/javascript" src="js/m35_dhjb.js"></script>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { lat: 36.045934, lng: 113.942816, alt: 1663, heading: 2, pitch: -25 },
          },
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        initUI();

        initEditorJS();
      }

      //UI界面相关
      function initUI() {
        $("#btn_start").click(function () {
          dataWork.start();
        });

        $("#btn_pause").click(function () {
          dataWork.pause();
        });

        $("#btn_proceed").click(function () {
          dataWork.proceed();
        });

        $("#btn_stop").click(function () {
          dataWork.stop();
        });

        map.on(mars3d.EventType.keydown, (event) => {
          switch (event.keyCode) {
            default:
              break;
            case "1".charCodeAt(0): //播放、暂停切换
              if (dataWork._isStart) {
                $("#btn_pause").click();
              } else {
                $("#btn_proceed").click();
              }
              break;
            case "2".charCodeAt(0): //停止
              $("#btn_stop").click();
              break;
            case "3".charCodeAt(0): //绑定和解绑键盘(控制器)控制
              map.keyboardRoam.enabled = !map.keyboardRoam.enabled;
              if (map.keyboardRoam.enabled) {
                haoutil.msg("已开启键盘控制");
              } else {
                haoutil.msg("已关闭键盘控制，您可以鼠标进行操作了！");
              }
              break;
          }
        });
      }

      //数据处理，及自动播放
      var dataWork = {
        alltimes: 0,
        arrNode: [],
        analysisData: function (arr) {
          for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            // item.state = { disabled: true }; //前端播放时禁用单击

            if (item.widget) {
              item.index = this.arrNode.length; //this.getNextId();
              item.id = item.index;
              item.times = item.times || 60;
              item.text = item.text + "(" + item.times + "秒)";
              item.icon = "fa fa-tag";
              this.arrNode.push(item);
              this.alltimes += item.times;
            }

            if (item.children) {
              this.analysisData(item.children);
            }
          }
        },
        steptimes: 0,
        initData: function (data) {
          this.analysisData(data);
          $("#alltimes").html(haoutil.str.formatTime(this.alltimes));

          setInterval(() => {
            if (!this._isStart) {
              return;
            }
            this.steptimes++;
            $("#thistimes").html(this.steptimes + "秒");
          }, 1000);

          $("#treeOverlays").jstree({
            core: {
              data: data,
              themes: {
                name: "default-dark",
                dots: true,
                icons: true,
              },
            },
          });

          $("#treeOverlays").on("changed.jstree", (e, data) => {
            var node = data.node.original;
            if (node && node.widget) {
              this.start(node);
            }
          });
        },
        _isStart: false,
        selectdNode: null,
        //开始
        start: function (node) {
          this.stop();

          $("#btn_start").hide();
          $("#btn_pause").show();
          $("#btn_proceed").hide();
          $("#btn_stop").show();

          this._isStart = true;
          map.clock.shouldAnimate = true;
          dataWork.activateNode(node || this.arrNode[0]);
        },
        //暂停
        pause: function () {
          $("#btn_start").hide();
          $("#btn_pause").hide();
          $("#btn_proceed").show();
          $("#btn_stop").show();

          this._isStart = false;
          map.cancelFlight();
          JB.stopRotatePoint();

          map.clock.shouldAnimate = false;

          if (this.timeIdx && this.timeIdx != -1) {
            clearTimeout(this.timeIdx);
            this.timeIdx = -1;
          }
        },
        //继续
        proceed: function () {
          $("#btn_start").hide();
          $("#btn_pause").show();
          $("#btn_proceed").hide();
          $("#btn_stop").show();

          map.clock.shouldAnimate = true;

          var node = this.selectdNode;
          if (node) {
            var that = this;

            this.steptimes = 0;
            node.widget.disable();
            node.widget.activate();

            this.timeIdx = setTimeout(function () {
              node.widget.disable();
              that.activateNextNode(node.index);
            }, node.times * 1000);
          } else {
            this.start();
          }
          this._isStart = true;
        },

        //停止
        stop: function () {
          this.pause();
          $("#btn_start").show();
          $("#btn_pause").hide();
          $("#btn_stop").hide();
          $("#btn_proceed").hide();

          $("#thisStep").html("无");
          $("#thistimes").html("");
          $("#treeOverlays").jstree("deselect_all", true);

          if (this.selectdNode) {
            this.selectdNode.widget.disable();
          }
          dataWork.selectdNode = null;
          this._isStart = false;

          JB.closePanel();
        },
        activateNode: function (node) {
          this.selectdNode = node;
          $("#stopRoate").val("暂停");
          this.steptimes = 0;
          $("#thisStep").html(node.text);
          $("#treeOverlays").jstree("deselect_all", true);
          $("#treeOverlays").jstree("select_node", node.id, true);
          node.widget.activate(node);

          // $("#treeOverlays").jstree().close_all(); //收起树

          var that = this;
          this.timeIdx = setTimeout(function () {
            node.widget.disable();
            that.activateNextNode(node.index);
          }, node.times * 1000);
        },
        activateNextNode: function (index) {
          index++;
          if (index < 0 || index >= this.arrNode.length) {
            this.stop();
            this.selectdNode = null;
            return;
          }
          var node = this.arrNode[index];
          this.activateNode(node);
        },
      };

      //通用方法 脚本列表
      var JB = {
        showPanel(html) {
          $("#viewResult").remove();
          var innerHTML = `<div id="viewResult" class="infoview viewResult" >
            ${html}
        </div>`;
          $("body").append(innerHTML);
        },
        closePanel() {
          $("#viewResult").remove();
        },
        playMP3(src) {
          var mp3 = new Audio(src);
          mp3.play(); //播放 mp3这个音频对象
          return mp3;
        },
        //绕点飞行
        startRotatePoint(center) {
          if (!this.rotatePoint) {
            this.rotatePoint = new mars3d.thing.RotatePoint({
              direction: false, //方向 true逆时针，false顺时针
              time: 50, //给定飞行一周所需时间(单位 秒)，控制速度
            });
            map.addThing(this.rotatePoint);
          }
          //开启旋转
          this.rotatePoint.start(center);
        },
        stopRotatePoint() {
          if (this.rotatePoint) {
            this.rotatePoint.stop();
          }
        },
      };
    </script>
  </body>
</html>
